#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate Prolog and Epilog Environment Variables
############################################################################
# Copyright (C) 2020 SchedMD LLC
# Written by Michael Hinton <hinton@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <http://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set exit_code       0
set prolog          "test$test_id\_prolog.sh"
set epilog          "test$test_id\_epilog.sh"
set prolog_out      "test$test_id\_prolog.out"
set epilog_out      "test$test_id\_epilog.out"
set test_node       [get_idle_node_in_part]
set job_id          0
set skipped         ""

proc cleanup { } {
	global config_dir exit_code bin_rm
	global prolog epilog prolog_out epilog_out

	restore_conf $config_dir/slurm.conf
	reconfigure
	exec $bin_rm -f $prolog $epilog $prolog_out $epilog_out
}

proc check_prolog { pattern } {
	global prolog_out exit_code bin_grep

	set rc [wait_for_command -timeout 5 "$bin_grep" "$pattern $prolog_out" $pattern]
	if { $rc == 0 } {
		log_info "Prolog: Found '$pattern'"
	} else {
		log_error "Prolog: Could not find '$pattern'!"
		set exit_code 1
	}
}

proc check_epilog { pattern } {
	global epilog_out exit_code bin_grep

	set rc [wait_for_command -timeout 5 "$bin_grep" "$pattern $epilog_out" $pattern]
	if { $rc == 0 } {
		log_info "Epilog: Found '$pattern'"
	} else {
		log_error "Epilog: Could not find '$pattern'!"
		set exit_code 1
	}
}

# Get the location of the slurm.conf file
set config_dir [get_conf_path]

#
# Copy slurm.conf file
#
save_conf $config_dir/slurm.conf

set cwd [$bin_pwd]
make_bash_script $prolog "
env > $cwd\/$prolog_out
"
make_bash_script $epilog "
env > $cwd\/$epilog_out
"

# Ensure that Prolog, Epilog, and nohold_on_prolog_fail params are removed
exec $bin_sed -i {s/^\(Prolog=\)/#\1/gI} $config_dir/slurm.conf
exec $bin_sed -i {s/^\(Epilog=\)/#\1/gI} $config_dir/slurm.conf
exec $bin_sed -i {s/nohold_on_prolog_fail//gI} $config_dir/slurm.conf

# Add \n in case the last line is commented out
exec $bin_echo "" >> $config_dir/slurm.conf

# Append Prolog and Epilog config to the slurm.conf
exec $bin_echo "Prolog=$cwd/$prolog" >> $config_dir/slurm.conf
exec $bin_echo "Epilog=$cwd/$epilog" >> $config_dir/slurm.conf
reconfigure -fail

# Test whether env vars exist in Prolog and Epilog
log_info "Checking for regular env vars"
spawn $sbatch -t1 -N1 -w$test_node -o/dev/null --wrap="hostname"
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}
if { !$job_id } {
	fail "Error submitting the batch job"
}

wait_for_job $job_id "DONE"

check_prolog "SLURM_CLUSTER_NAME="
check_epilog "SLURM_CLUSTER_NAME="

check_prolog "SLURM_JOB_CONSTRAINTS="

check_prolog "SLURM_JOB_ID="
check_epilog "SLURM_JOB_ID="

check_prolog "SLURM_JOB_PARTITION="

check_prolog "SLURM_JOB_UID="
check_epilog "SLURM_JOB_UID="

check_prolog "SLURM_JOB_USER="
check_epilog "SLURM_JOB_USER="

check_prolog "SLURM_SCRIPT_CONTEXT=prolog_slurmd"
check_epilog "SLURM_SCRIPT_CONTEXT=epilog_slurmd"

set test_node [get_nodes_by_request "--gres=gpu:1 -t1"]
if {[llength $test_node] != 1} {
	lappend skipped "This test requires being able to submit job with --gres=gpu:1"
} else {
	log_info "Checking for GPU-related env vars"
	spawn $sbatch -t1 -N1 -w$test_node -o/dev/null --wrap="hostname" --gres=gpu:1
	expect {
		-re "Submitted batch job ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "sbatch is not responding"
		}
		eof {
			wait
		}
	}
	wait_for_job $job_id "DONE"

	check_prolog "CUDA_VISIBLE_DEVICES="
	check_epilog "CUDA_VISIBLE_DEVICES="
	check_prolog "GPU_DEVICE_ORDINAL="
	check_epilog "GPU_DEVICE_ORDINAL="
	# TODO: Check for SLURM_JOB_GPUS once patch 8596 19.05 v1 lands
	# check_prolog "SLURM_JOB_GPUS="
	# check_epilog "SLURM_JOB_GPUS="
	# TODO: Check for ROCR_VISIBLE_DEVICES once patch 8596 20.11 v1 lands
	# check_prolog "ROCR_VISIBLE_DEVICES="
	# check_epilog "ROCR_VISIBLE_DEVICES="
}

set test_node [get_nodes_by_request "--gres=mps:1 -t1"]
if {[llength $test_node] != 1} {
	lappend skipped "This test requires being able to submit job with --gres=mps:1"
} else {
	log_info "Checking for MPS-related env vars"
	spawn $sbatch -t1 -N1 -w$test_node -o/dev/null --wrap="hostname" --gres=mps:1
	expect {
		-re "Submitted batch job ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "sbatch is not responding"
		}
		eof {
			wait
		}
	}
	wait_for_job $job_id "DONE"

	check_prolog "CUDA_MPS_ACTIVE_THREAD_PERCENTAGE="
	check_epilog "CUDA_MPS_ACTIVE_THREAD_PERCENTAGE="
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}

if {[llength $skipped]} {
	skip [join $skipped "\n"]
}
